package com.ruoyi.qiankuantongji.controller;

import java.util.ArrayList;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.qiankuantongji.domain.Qiankuantongji;
import com.ruoyi.qiankuantongji.service.IQiankuantongjiService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.qiankuantongji.domain.chaxunRuselt;

import javax.servlet.http.HttpServletRequest;

/**
 * 欠款统计Controller
 * 
 * @author ruoyi
 * @date 2023-02-23
 */
@Controller
@RequestMapping("/qiankuantongji/qiankuantongjiTest")
public class QiankuantongjiController extends BaseController
{
    private String prefix = "qiankuantongji/qiankuantongjiTest";

    @Autowired
    private IQiankuantongjiService qiankuantongjiService;

    @RequiresPermissions("qiankuantongji:qiankuantongjiTest:view")
    @GetMapping()
    public String qiankuantongjiTest()
    {
        return prefix + "/qiankuantongjiTest";
    }

    /**
     * 查询欠款统计列表
     */
    @RequiresPermissions("qiankuantongji:qiankuantongjiTest:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request, Qiankuantongji qiankuantongji)
    {
        startPage();
        String a = request.getSession().getAttribute("QuanXian").toString();
        String b=request.getSession().getAttribute("name4user").toString();
        if (a.equals("0") && (qiankuantongji == null || qiankuantongji.getCangku().equals(""))) {
            List<chaxunRuselt> namee2cangku = qiankuantongjiService.selectAllQiankuantongjiList(b);
            return getDataTable(namee2cangku);
        }else {
            startPage();
            List<chaxunRuselt> list;
            if (qiankuantongji.getCangku() != null && !qiankuantongji.getCangku().equals("")) {
                list = qiankuantongjiService.selectAllQiankuantongjiList(qiankuantongji.getCangku());
            }else {
                list = qiankuantongjiService.selectAllQiankuantongjiList(null);
            }

            return getDataTable(list);
        }

    }

    /**
     * 导出欠款统计列表
     */
    @RequiresPermissions("qiankuantongji:qiankuantongjiTest:export")
    @Log(title = "欠款统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Qiankuantongji qiankuantongji)
    {
        List<Qiankuantongji> list = qiankuantongjiService.selectQiankuantongjiList(qiankuantongji);
        ExcelUtil<Qiankuantongji> util = new ExcelUtil<Qiankuantongji>(Qiankuantongji.class);
        return util.exportExcel(list, "欠款统计数据");
    }

    /**
     * 新增欠款统计
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存欠款统计
     */
    @RequiresPermissions("qiankuantongji:qiankuantongjiTest:add")
    @Log(title = "欠款统计", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Qiankuantongji qiankuantongji)
    {
        return toAjax(qiankuantongjiService.insertQiankuantongji(qiankuantongji));
    }

    /**
     * 修改欠款统计
     */
    @RequiresPermissions("qiankuantongji:qiankuantongjiTest:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Qiankuantongji qiankuantongji = qiankuantongjiService.selectQiankuantongjiById(id);
        mmap.put("qiankuantongji", qiankuantongji);
        return prefix + "/edit";
    }

    /**
     * 修改保存欠款统计
     */
    @RequiresPermissions("qiankuantongji:qiankuantongjiTest:edit")
    @Log(title = "欠款统计", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Qiankuantongji qiankuantongji)
    {
        return toAjax(qiankuantongjiService.updateQiankuantongji(qiankuantongji));
    }

    /**
     * 删除欠款统计
     */
    @RequiresPermissions("qiankuantongji:qiankuantongjiTest:remove")
    @Log(title = "欠款统计", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(qiankuantongjiService.deleteQiankuantongjiByIds(ids));
    }
}
